import { Component, OnInit } from '@angular/core';
import { Farm } from "../../../entity/farm.model";
import { FarmService } from "../../../service/farm.service";
import { LossElectricityService } from "../../../service/loss-electricity.service";

@Component({
  selector: 'app-loss-electricity',
  templateUrl: './loss-electricity.component.html',
  styleUrls: ['./loss-electricity.component.css']
})
export class LossElectricityComponent implements OnInit {
  //时间选择
  public years: string[] = ["2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013", "2012", "2011", "2010"]
  public currentYear: string = this.years[3];
  public months: string[] = ["12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"];
  public currentMonth: string = this.months[0];

  //当前风场
  public farms: Farm[] = [];
  public currentFarm: Farm;

  //日期类型
  public dateType: number = 3;
  /**
	 * 选中的年度或者月度，0：年度，1：月度
	 */
  // active: number = 0;

  xAxisData: string[] = [];//X轴数据
  XAxisDataTurbine: string[] = [];//风机图表X轴数据
  seriesLineData: number[] = [];//折线series数据集合
  seriesColumnData: number[][] = [];//柱状图series数据集合
  seriesData: number[][] = [];//风机series数据集合

  seriesLine = [];//能量利用率series
  seriesColumn = [];//柱状图series
  series = [];//风机图表的series

  show1: boolean = false;
  show2: boolean = true;//折线图表的Y轴是否显示
  position1: string = "left";
  position2: string = "left";//折线图表的Y轴的位置

  show3: boolean = true;
  show4: boolean = false;//折线图表的Y轴是否显示
  position3: string = "left";
  position4: string = "right";//折线图表的Y轴的位置

  show5: boolean = true;
  show6: boolean = true;//折线图表的Y轴是否显示
  position5: string = "left";
  position6: string = "right";//折线图表的Y轴的位置
  constructor(
    private farmService: FarmService,
    private lossElectricityService: LossElectricityService,
  ) { }

  ngOnInit() {
    this.farmService.getFarms()
      .then(res => {
        if (res) {
          this.farms = [...res];
          this.currentFarm = this.farms[0];
          this.onSearch(this.dateType)
        }
      })
      .catch(err => {
        console.log(err)
      })
  }

  //风场点击事件
  onFarmClick(farm: Farm) {
    this.currentFarm = farm;
    this.onSearch(this.dateType);
  }
  //点击年度或月度按钮
  public onClickHeader(n: number) {
    this.dateType = n;
    this.onSearch(this.dateType)
  }
  //年度改变
  public onChangeYear(event) {
    this.currentYear = event;
  }
  //月度改变
  public onChangeMonth(event) {
    this.currentMonth = event;
  }
  //查询
  public onSearch(dateType: number) {
    this.initEchartsData(dateType);
  }
  //初始化图标
  initEchartsData(dateType: number) {
    //点击年度
    if (dateType == 3) {

      /**
       * let time = new Date(this.currentYear + '-' + this.currentMonth);
       * */
      this.lossElectricityService.getFarmPowerLossElectricity(this.currentFarm.farmId, Number(this.currentYear), 0)
        .then(res => {
          // let res = {//年度
          //   farm: {
          //     realPowers: [//实发电量
          //       { date: '2017-01-02', id: 21, name: '南澳', value: 100 },
          //       { date: '2017-02-03', id: 21, name: '南澳', value: 200 },
          //     ],
          //     lossPowers: [//损失电量
          //       { date: '2017-01-02', id: 21, name: '南澳', value: 100 },
          //       { date: '2017-02-03', id: 21, name: '南澳', value: 200 },
          //     ]
          //   },
          //   turbines: {
          //     realPowers: [//实发电量
          //       { date: '2017-01-02', id: 21, name: '#1', value: 100 },
          //       { date: '2017-02-03', id: 21, name: '#2', value: 200 },
          //     ],
          //     lossPowers: [//损失电量
          //       { date: '2017-01-02', id: 21, name: '#1', value: 100 },
          //       { date: '2017-02-03', id: 21, name: '#2', value: 200 },
          //     ]
          //   }
          // }
          this.seriesColumnData = [[], []];//柱状图的series
          this.seriesData = [[], [], []];//柱状图和折线的series
          this.seriesLineData = [];//折线图的series
          this.xAxisData = [];
          this.XAxisDataTurbine = [];

          //实发电量
          res.farm.realPowers.forEach(realPower => {
            // console.log(realPower.value,realPower.value/10000)
            realPower.date = realPower.date.split("-")[1].toString();
            
            this.seriesColumnData[0].push(Number((realPower.value/10000).toFixed(2)));
          })

          //损失电量
          res.farm.lossPowers.forEach(lossPower => {
            
            this.seriesColumnData[1].push(Number((lossPower.value/10000).toFixed(2)));
            
            this.xAxisData.push(lossPower.date.split("-")[1] + '月');//X轴坐标
          })
          //能量利用率
          for (let i = 0; i <= this.seriesColumnData[0].length; i++) {
            if((this.seriesColumnData[0][i] + this.seriesColumnData[1][i]) * 100 == 0 ){
               this.seriesLineData.push(0)
            }else{
              
              this.seriesLineData.push(Number((this.seriesColumnData[0][i] / (this.seriesColumnData[0][i] + this.seriesColumnData[1][i]) * 100).toFixed(2)));
            }
            
          }

          //风机实发电量
          res.turbine.realPowers.forEach(realPower => {
            realPower.date = realPower.date.split("-")[2].toString();
            
            this.seriesData[0].push(Number((realPower.value/10000).toFixed(2)));
          })

          //风机损失电量
          res.turbine.lossPowers.forEach(lossPower => {
            
            this.seriesData[1].push(Number((lossPower.value/10000).toFixed(2)));
            this.XAxisDataTurbine.push(lossPower.name);//X轴坐标
          })
          //风机能量利用率
          for (let i = 0; i < this.seriesData[0].length; i++) {
            if((this.seriesData[0][i] + this.seriesData[1][i]) * 100 == 0){
              this.seriesData[2].push(0);
            }else{
              
              this.seriesData[2].push(Number((this.seriesData[0][i] / (this.seriesData[0][i] + this.seriesData[1][i]) * 100).toFixed(2)));
            }
            
          }


          //风机的series
          this.series = [
            {
              name: '实发电量',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'inside'
                }
              },
              data: this.seriesData[0]
            },
            {
              name: '损失电量',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'inside'
                }
              },
              data: this.seriesData[1]
            },

            {
              name: '能量利用率',
              type: 'line',
              yAxisIndex: 1,
              data: this.seriesData[2]
            }
          ]
          //折线的series
          this.seriesLine = [
            {
              name: '能量利用率',
              type: 'line',
              yAxisIndex: 1,
              data: this.seriesLineData
            }
          ];
          //柱状图的series
          this.seriesColumn = [
            {
              name: '实发电量',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'inside'
                }
              },
              data: this.seriesColumnData[0]
            },
            {
              name: '损失电量',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'inside'
                }
              },
              data: this.seriesColumnData[1]
            }
          ];

        })
        .catch(error => {
          console.log("app-loss-electricity", error);
        });


    }
    //点击月度
    else {
      /**
       * let time = new Date(this.currentYear + '-' + this.currentMonth);
       * */

      this.lossElectricityService.getFarmPowerLossElectricity(this.currentFarm.farmId, Number(this.currentYear), Number(this.currentMonth))
        .then(res => {
          // let res = {//月度
          //   farm: {
          //     realPowers: [//实发电量
          //       { date: '2017-01-02', id: 21, name: '南澳', value: 100 },
          //       { date: '2017-02-03', id: 21, name: '南澳', value: 200 },
          //     ],
          //     lossPowers: [//损失电量
          //       { date: '2017-01-02', id: 21, name: '南澳', value: 100 },
          //       { date: '2017-02-03', id: 21, name: '南澳', value: 200 },
          //     ]
          //   },
          //   turbines: {
          //     realPowers: [//实发电量
          //       { date: '2017-01-02', id: 21, name: '#1', value: 200 },
          //       { date: '2017-02-03', id: 21, name: '#2', value: 500 },
          //     ],
          //     lossPowers: [//损失电量
          //       { date: '2017-01-02', id: 21, name: '#1', value: 500 },
          //       { date: '2017-02-03', id: 21, name: '#2', value: 200 },
          //     ]
          //   }
          // }
          this.seriesColumnData = [[], []];
          this.seriesData = [[], [], []];
          this.xAxisData = [];
          this.seriesLineData = [];
          this.XAxisDataTurbine = [];
          //风场实发电量
          res.farm.realPowers.forEach(realPower => {
            realPower.date = realPower.date.split("-")[2].toString();
            this.seriesColumnData[0].push(Number((realPower.value/10000).toFixed(2)));            
          })
          //风场损失电量
          res.farm.lossPowers.forEach(lossPower => {            
            this.seriesColumnData[1].push(Number((lossPower.value/10000).toFixed(2)));
            this.xAxisData.push(lossPower.date.split("-")[2] + '日');//X轴坐标
          })
          //风场能量利用率
          for (let i = 0; i < this.seriesColumnData[0].length; i++) {
            if((this.seriesColumnData[0][i] + this.seriesColumnData[1][i]) * 100 == 0){
              this.seriesLineData.push(0);
            }else{
              
              this.seriesLineData.push(Number((this.seriesColumnData[0][i] / (this.seriesColumnData[0][i] + this.seriesColumnData[1][i]) * 100).toFixed(2)));
            }
            
          }

          //风机实发电量
          res.turbine.realPowers.forEach(realPower => {
            realPower.date = realPower.date.split("-")[2].toString();
            
            this.seriesData[0].push(Number((realPower.value/10000).toFixed(2)));
          })

          //风机损失电量
          res.turbine.lossPowers.forEach(lossPower => {
            this.seriesData[1].push(Number((lossPower.value/10000).toFixed(2)));
            this.XAxisDataTurbine.push(lossPower.name);//X轴坐标
          })
          //风机能量利用率
          for (let i = 0; i < this.seriesData[0].length; i++) {
            if((this.seriesData[0][i] + this.seriesData[1][i]) * 100 == 0 ){
              this.seriesData[2].push(0);
            }else{
              
              this.seriesData[2].push(Number((this.seriesData[0][i] / (this.seriesData[0][i] + this.seriesData[1][i]) * 100).toFixed(2)));
            }
            
          }

          //风机的series
          this.series = [
            {
              name: '实发电量',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'inside'
                }
              },
              data: this.seriesData[0]
            },
            {
              name: '损失电量',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'inside'
                }
              },
              data: this.seriesData[1]
            },

            {
              name: '能量利用率',
              type: 'line',
              yAxisIndex: 1,
              data: this.seriesData[2]
            }
          ]
          //折线的series
          this.seriesLine = [
            {
              name: '能量利用率',
              type: 'line',
              yAxisIndex: 1,
              data: this.seriesLineData
            }
          ];
          //柱状图的series
          this.seriesColumn = [
            {
              name: '实发电量',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'inside'
                }
              },
              data: this.seriesColumnData[0]
            },
            {
              name: '损失电量',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'inside'
                }
              },
              data: this.seriesColumnData[1]
            }
          ];

        })
        .catch(error => {
          console.log("app-loss-electricity", error);
        });


    }
  }

}



// WEBPACK FOOTER //
// ./src/app/module/data-analysis/loss-electricity/loss-electricity.component.ts